import * as types from '../actionTypes/todos'

// 删除任务action
// export const delTodo = id => {
//   return {
//     type: types.DEL_TODO,
//     payload: id
//   }
// }

// thunk action
export const delTodo = id => {
  // dispatch (redux的dipatch) getState 获取redux状态
  return (dispatch, getState) => {
    // 异步操作
    setTimeout(() => {
      // 后台已经删除
      // redux也要跟新
      dispatch({ type: types.DEL_TODO, payload: id })
    }, 2000)
  }
}


// 切换任务状态
export const toggleDone = (id, checked) => {
  return {
    type: types.TOGGLE_DONE,
    payload: {
      id,
      done: checked
    }
  }
}

// 添加任务
export const addTodo = (text) => {
  return {
    type: types.ADD_TODO,
    payload: text
  }
}

// 切换全选
export const toggleAll = (checked) => {
  return {
    type: types.TOGGLE_ALL,
    payload: checked
  }
}

// 清空已完成
export const clearDone = () => {
  return { type: types.CLEAR_DONE }
}

// 修改任务
export const updateTodo = (id, todoName) => {
  return {
    type: types.UPDATE_TODO,
    payload: { id, text: todoName }
  }
}